using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System.Reflection;
using UnityEngine.UI;
using Newtonsoft.Json;

public class ConfigTool
{
	public static string[] bundleDir = new string[]{ResourcesEx.BundleResDir};

	// public static void rebuildBundleConfig()
	// {
	// 	createPlatfromBundleConfig2(EditorUserBuildSettings.activeBuildTarget, SwitchConfig.isEnableSpriteSheet);
	// }

	public static void deleteNotUsedFiles()
	{
		BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
		BundleConfig config = EditorCommonTool.getBundleConfig(target);
		HashSet<string> bundles = new HashSet<string>();
//		config.getGroupBundles("evony1", ref bundles);
		config.getGroupBundles("evony2", ref bundles);

		HashSet<string> allBundles = config.getAllBundles();

		foreach(string bundleName in bundles)
		{
			allBundles.Remove(bundleName);
		}

		foreach(string bundleName in allBundles)
		{
			Bundle bundle = config.getBundle(bundleName);
			for(int i = 0; i < bundle.dirs.Length; i++)
			{
				string dir = bundle.dirs[i];
				string path = bundleDir[0] + "/" + dir;
				CommonUtil.Delete(path);
			}
		}
	}

	[MenuItem("Custom/OldProject/Config/CreatePlatformBundleConfig")]
	public static void createPlatfromBundleConfig()
	{
		createPlatfromBundleConfig2(BuildTarget.Android);
		createPlatfromBundleConfig2(EditorCommonTool.build_target_ios);
	}

	public static void createPlatfromBundleConfig2(BuildTarget target, bool useSpriteSheet = true)
	{
        string resPath = bundleDir[0];
		string str = CommonUtil.fileToString(resPath + "/" + "raw_" + BundleManager.configName + ".json");

		string ConfigStr = str.Replace("${platform}", target.getPlatform().ToString());
		BundleConfig config = JsonConvert.DeserializeObject<BundleConfig>(ConfigStr);

//		if(icp)
//		{
//			{
//				BundleGroup group = config.getBundleGroup("total_base");
//				List<string> arr = new List<string>(group.groups);
//				arr.Add(ResourcesEx.IcpResDirName);
//				group.groups = arr.ToArray();
//			}
//			{
//				BundleGroup group = config.getBundleGroup("local");
//				List<string> arr = new List<string>(group.groups);
//				arr.Add(ResourcesEx.IcpResDirName);
//				group.groups = arr.ToArray();
//			}
//		}

		SpriteSheetConfig spriteSheetConfig = EditorCommonTool.getSpriteSheetConfig(resPath, target.getPlatform());
		if(!useSpriteSheet)
		{
//			for(int i = 0; i < config.bundles.Length; i++)
//			{
//				BundleLayer bundleLayer2 = config.bundles[i];
//				
//				foreach(Bundle bundle2 in bundleLayer2.Values)
//				{
//					HashSet<string> newDirs = new HashSet<string>();
//					for(int j = 0; j < bundle2.dirs.Length; j++)
//					{
//						string dir2 = bundle2.dirs[j];
//						if(dir2.StartsWith(EditorUserBuildSettings.activeBuildTarget.getPlatform() + "/" + ResourcesEx.SheetDirName + "/"))
//						{
//							string spriteSheetName = dir2.Split(new char[]{'/'})[2];
//							SpriteSheetInfo info = null;
//							spriteSheetConfig.TryGetValue(spriteSheetName, out info);
//							if(info.dirs != null)
//							{
//								for(int k = 0; k < info.dirs.Length; k++)
//								{
//									newDirs.Add(info.dirs[k]);
//								}
//							}
////							Dictionary<string, SpriteSheetInfo> dic = new Dictionary<string, SpriteSheetInfo>();
////							spriteSheetConfig.getSpriteSheetGroup(spriteSheetName, ref dic);
////							foreach(SpriteSheetInfo info in dic.Values)
////							{
////								if(info.dirs != null)
////								{
////									for(int k = 0; k < info.dirs.Length; k++)
////									{
////										newDirs.Add(info.dirs[k]);
////									}
////								}
////							}
//						}
//						else
//						{
//							newDirs.Add(dir2);
//						}
//					}
//					
//					bundle2.dirs = new List<string>(newDirs).ToArray();
//				}
//			}

                BundleLayer bundleLayer2 = config.bundles;

                foreach(Bundle bundle2 in bundleLayer2.Values)
                {
                    HashSet<string> newDirs = new HashSet<string>();
                    for(int j = 0; j < bundle2.dirs.Length; j++)
                    {
                        string dir2 = bundle2.dirs[j];
                        if(dir2.StartsWith(EditorUserBuildSettings.activeBuildTarget.getPlatform() + "/" + ResourcesEx.SheetDirName + "/"))
                        {
                            string spriteSheetName = dir2.Split(new char[]{'/'})[2];
                            SpriteSheetInfo info = null;
                            spriteSheetConfig.TryGetValue(spriteSheetName, out info);
                            if(info.dirs != null)
                            {
                                for(int k = 0; k < info.dirs.Length; k++)
                                {
                                    newDirs.Add(info.dirs[k]);
                                }
                            }
                            //                          Dictionary<string, SpriteSheetInfo> dic = new Dictionary<string, SpriteSheetInfo>();
                            //                          spriteSheetConfig.getSpriteSheetGroup(spriteSheetName, ref dic);
                            //                          foreach(SpriteSheetInfo info in dic.Values)
                            //                          {
                            //                              if(info.dirs != null)
                            //                              {
                            //                                  for(int k = 0; k < info.dirs.Length; k++)
                            //                                  {
                            //                                      newDirs.Add(info.dirs[k]);
                            //                                  }
                            //                              }
                            //                          }
                        }
                        else
                        {
                            newDirs.Add(dir2);
                        }
                    }

                    bundle2.dirs = new List<string>(newDirs).ToArray();
                }
		}

		Dictionary<string, HashSet<string>> changeBundles = new Dictionary<string, HashSet<string>>();
		HashSet<string> set = new HashSet<string>();
		
		{
			BundleLayer layer = config.bundles;
			BundleLayer newLayer = new BundleLayer();
			
			Dictionary<string, Bundle>.Enumerator enumerator = layer.GetEnumerator();
			while(enumerator.MoveNext())
			{
				Bundle bundle = enumerator.Current.Value;
				string[] dirs = bundle.dirs;
				for(int k = 0; k < dirs.Length; k++)
				{
					string dir2 = dirs[k];
					if(dir2.Contains("/" + ResourcesEx.SheetDirName + "/"))
					{
						string spriteSheetParentPath = Path.GetDirectoryName(dir2);
						string spriteSheetName = Path.GetFileName(dir2);
						SpriteSheetInfo rootInfo = null;
						spriteSheetConfig.TryGetValue(spriteSheetName, out rootInfo);
						
						if(rootInfo == null)
						{
							Debug.LogError("not found sprite sheet info: " + spriteSheetName);
							return;
						}
						
						set.Add(spriteSheetName);
						
						
						bool addLeaf = false;
						Dictionary<string, SpriteSheetInfo> dic = new Dictionary<string, SpriteSheetInfo>();
						spriteSheetConfig.getSpriteSheetGroup(spriteSheetName, ref dic);
						Dictionary<string, SpriteSheetInfo>.Enumerator e2 = dic.GetEnumerator();
						while(e2.MoveNext())
						{
							if(e2.Current.Value.isLeaf())
							{
								string subSpriteSheetPath = spriteSheetParentPath + "/" + e2.Current.Key;
								if(!Directory.Exists(resPath + "/" + subSpriteSheetPath))
								{
									Debug.LogError("create bundle failed: directory not found: " + resPath + "/" + subSpriteSheetPath);
								}
								
								Bundle newBundle = new Bundle();
								newBundle.dirs = new string[]{subSpriteSheetPath};
								newLayer.Add(e2.Current.Key, newBundle);
								
								HashSet<string> temp = null;
								if(!changeBundles.TryGetValue(enumerator.Current.Key, out temp))
								{
									temp = new HashSet<string>();
									changeBundles.Add(enumerator.Current.Key, temp);
								}
								temp.Add(e2.Current.Key);
								
								addLeaf = true;
							}
						}
						
						if(!addLeaf)
						{
							Debug.LogError("create bundle failed: sprite sheet not found: " + spriteSheetName);
						}
					}		
					else
					{
						if(!newLayer.ContainsKey(enumerator.Current.Key))
						{
							newLayer.Add(enumerator.Current.Key, enumerator.Current.Value);
						}
					}
				}
			}
			
			config.bundles = newLayer;
		}






		//check
		{
			if(useSpriteSheet)
			{
				SpriteSheetConfig.Enumerator e = spriteSheetConfig.GetEnumerator();
				while(e.MoveNext())
				{
					if(e.Current.Value.isRoot())
					{
						if(!set.Contains(e.Current.Key))
						{
							Debug.LogError("not found bundle info:" + e.Current.Key);
							return;
						}
					}
				}
			}

			HashSet<string> allBundles = new HashSet<string>();

			BundleLayer layer = config.bundles;
			
			Dictionary<string, Bundle>.Enumerator enumerator = layer.GetEnumerator();
			while(enumerator.MoveNext())
			{
				allBundles.Add(enumerator.Current.Key);
			}
			
			Dictionary<string, BundleGroup>.Enumerator groupEnumerator3 = config.groups.GetEnumerator();
			while(groupEnumerator3.MoveNext())
			{
				BundleGroup bundleGroup = groupEnumerator3.Current.Value;
				
				if(bundleGroup.bundles != null)
				{
					List<string> list = new List<string>(bundleGroup.bundles);
					
					for(int j = 0; j < bundleGroup.bundles.Length; j++)
					{
						string bundleName = bundleGroup.bundles[j];
						HashSet<string> temp = null;
						if(changeBundles.TryGetValue(bundleName, out temp))
						{
							list.Remove(bundleName);
							list.AddRange(temp);
						}
					}
					
					list.Sort();
					bundleGroup.bundles = list.ToArray();
				}
			}

			HashSet<string> existBundles = new HashSet<string>();
			Dictionary<string, BundleGroup>.Enumerator groupEnumerator2 = config.groups.GetEnumerator();
			while(groupEnumerator2.MoveNext())
			{
				BundleGroup bundleGroup = groupEnumerator2.Current.Value;
				
				if(bundleGroup.bundles != null)
				{
					for(int j = 0; j < bundleGroup.bundles.Length; j++)
					{
						string bundleName = bundleGroup.bundles[j];
						if(!allBundles.Contains(bundleName))
						{
							Debug.LogError("not found bundle: " + bundleName);
						}
						else
						{
							existBundles.Add(bundleName);
						}
					}
				}
			}
			
			foreach(string b in allBundles)
			{
				if(!existBundles.Contains(b))
				{
					Debug.LogError("not found bundle from bundle group: " + b);
				}
			}
		}

		//create scene config bundle
		{

			{
				BundleLayer layer = config.bundles;
				BundleLayer newBundleLayer = new BundleLayer();
				
				Dictionary<string, Bundle>.Enumerator enumerator = layer.GetEnumerator();
				while(enumerator.MoveNext())
				{
					Bundle bundle = enumerator.Current.Value;
					if(bundle.type == BundleType.scene)
					{
						string[] dirs = bundle.dirs;
						
						Bundle newBundle = new Bundle();
						string newBundleName = enumerator.Current.Key + BundleManager.sceneConfigBundleSuffix;
						newBundle.dirs = new string[dirs.Length];
						for(int k = 0; k < dirs.Length; k++)
						{
							newBundle.dirs[k] = dirs[k] + BundleManager.sceneBundleDependenceSuffix;
						}
						
						newBundleLayer.Add(newBundleName, newBundle);
					}
				}
				
				Dictionary<string, Bundle>.Enumerator newEnumerator = newBundleLayer.GetEnumerator();
				while(newEnumerator.MoveNext())
				{
					layer.Add(newEnumerator.Current.Key, newEnumerator.Current.Value);
				}
			}


			Dictionary<string, BundleGroup>.Enumerator groupEnumerator = config.groups.GetEnumerator();
			while(groupEnumerator.MoveNext())
			{
				BundleGroup bundleGroup = groupEnumerator.Current.Value;
				
				if(bundleGroup.bundles != null)
				{
					List<string> newBundles = new List<string>(bundleGroup.bundles);
					for(int j = 0; j < bundleGroup.bundles.Length; j++)
					{
						string bundleName = bundleGroup.bundles[j];
						Bundle bundle = config.getBundle(bundleName);
						if(bundle == null)
						{
							Debug.LogError("bundle not found:" + bundleName);
						}
						if(bundle.type == BundleType.scene)
						{
							newBundles.Add(bundleName + BundleManager.sceneConfigBundleSuffix);
						}
					}
					bundleGroup.bundles = newBundles.ToArray();
				}
			}
		}

		MemoryStream stream = new MemoryStream();
		BinaryWriter writer = new BinaryWriter(stream);
		config.Write(writer);
		
		string 	configPath = EditorCommonTool.getBundleConfigPath(target);
		string dir = Path.GetDirectoryName(configPath);
		Directory.CreateDirectory(dir);
		CommonUtil.bytesToFile(configPath, stream.ToArray());
		
//		if(ExportAssetBundles.createJson)
//		{
//			string newJsonStr = JsonConvert.SerializeObject(config, Formatting.Indented);
//			configPath = configPath.Replace(".bytes", "_json.json");
//			CommonUtil.stringToFile(configPath, newJsonStr);
//		}
//		
//		AssetDatabase.Refresh();
//		
//		ExportAssetBundles.CreateDependenceFiles(target);
	}

//	[MenuItem("Custom/OldProject/Config/CreatePlatformBundleConfig")]
//	public static void createPlatfromBundleConfig()
//	{
//		string resPath = CreateSpriteSheet.bundleDir;
//		string str = CommonUtil.fileToString(resPath + "/" + "raw_" + BundleManager.configName + ".json");
//
//
//		BuildTarget[] targets = new BuildTarget[]{BuildTarget.Android, EditorCommonTool.build_target_ios};
//		for(int i = 0; i < targets.Length; i++)
//		{
//			string ConfigStr = str.Replace("${platform}", EditorCommonTool.getPlatform(targets[i]).ToString());
//
//			BundleConfig config = JsonConvert.DeserializeObject<BundleConfig>(ConfigStr);
//
//			HashSet<string> set = new HashSet<string>();
//			SpriteSheetConfig spriteSheetConfig = EditorCommonTool.getSpriteSheetConfig(resPath, targets[i].getPlatform());
//			for(int j = 0; j < config.bundles.Length; j++)
//			{
//				BundleLayer layer = config.bundles[j];
//				Dictionary<string, Bundle>.Enumerator enumerator = layer.GetEnumerator();
//				while(enumerator.MoveNext())
//				{
//					Bundle bundle = enumerator.Current.Value;
//
//					List<string> newDirs = new List<string>();
//					string[] dirs = bundle.dirs;
//					for(int k = 0; k < dirs.Length; k++)
//					{
//						string dir2 = dirs[k];
//						if(dir2.Contains("/" + ResourcesEx.SheetDirName + "/"))
//						{
//							string spriteSheetParentPath = Path.GetDirectoryName(dir2);
//							string spriteSheetName = Path.GetFileName(dir2);
//							SpriteSheetInfo rootInfo = null;
//							spriteSheetConfig.TryGetValue(spriteSheetName, out rootInfo);
//
//							if(rootInfo == null)
//							{
//								Debug.LogError("not found sprite sheet info: " + spriteSheetName);
//								return;
//							}
//
//							set.Add(spriteSheetName);
//
////							for(int u = 0; u < rootInfo.dirs.Length; u++)
////							{
////								string dir3 = rootInfo.dirs[u];
////								string animPath = resPath + "/" + ResourcesEx.AnimDirName + "/" + dir3;
////								if(Directory.Exists(animPath))
////								{
////									newDirs.Add(ResourcesEx.AnimDirName + "/" + dir3);
////								}
////							}
//
//							bool addLeaf = false;
//							Dictionary<string, SpriteSheetInfo> dic = new Dictionary<string, SpriteSheetInfo>();
//							spriteSheetConfig.getSpriteSheetGroup(spriteSheetName, ref dic);
//							Dictionary<string, SpriteSheetInfo>.Enumerator e2 = dic.GetEnumerator();
//							while(e2.MoveNext())
//							{
//								if(e2.Current.Value.isLeaf())
//								{
//									string subSpriteSheetPath = spriteSheetParentPath + "/" + e2.Current.Key;
//									if(!Directory.Exists(resPath + "/" + subSpriteSheetPath))
//									{
//										Debug.LogError("create bundle failed: directory not found: " + resPath + "/" + subSpriteSheetPath);
//									}
//									newDirs.Add(subSpriteSheetPath);
//									addLeaf = true;
//								}
//							}
//
//							if(!addLeaf)
//							{
//								Debug.LogError("create bundle failed: sprite sheet not found: " + spriteSheetName);
//							}
//						}		
//						else
//						{
//							newDirs.Add(dir2);
//						}
//					}
//
//					bundle.dirs = newDirs.ToArray();
//				}
//			}
//
//			SpriteSheetConfig.Enumerator e = spriteSheetConfig.GetEnumerator();
//			while(e.MoveNext())
//			{
//				if(e.Current.Value.isRoot())
//				{
//					if(!set.Contains(e.Current.Key))
//					{
//						Debug.LogError("not found bundle info:" + e.Current.Key);
//						return;
//					}
//				}
//			}
//
//			HashSet<string> allBundles = new HashSet<string>();
//			for(int j = 0; j < config.bundles.Length; j++)
//			{
//				BundleLayer layer = config.bundles[j];
//				
//				Dictionary<string, Bundle>.Enumerator enumerator = layer.GetEnumerator();
//				while(enumerator.MoveNext())
//				{
//					allBundles.Add(enumerator.Current.Key);
//				}
//			}
//
//			Dictionary<string, BundleGroup>.Enumerator groupEnumerator2 = config.groups.GetEnumerator();
//			while(groupEnumerator2.MoveNext())
//			{
//				BundleGroup bundleGroup = groupEnumerator2.Current.Value;
//				
//				if(bundleGroup.bundles != null)
//				{
//					for(int j = 0; j < bundleGroup.bundles.Length; j++)
//					{
//						string bundleName = bundleGroup.bundles[j];
//						if(!allBundles.Contains(bundleName))
//						{
//							Debug.LogError("not found bundle: " + bundleName);
//						}
//						else
//						{
//							allBundles.Remove(bundleName);
//						}
//					}
//				}
//			}
//
//			foreach(string b in allBundles)
//			{
//				Debug.LogError("not found bundle from bundle group: " + b);
//			}
//
//
//			for(int j = 0; j < config.bundles.Length; j++)
//			{
//				BundleLayer layer = config.bundles[j];
//				BundleLayer newBundleLayer = new BundleLayer();
//
//				Dictionary<string, Bundle>.Enumerator enumerator = layer.GetEnumerator();
//				while(enumerator.MoveNext())
//				{
//					Bundle bundle = enumerator.Current.Value;
//					if(bundle.type == BundleType.scene)
//					{
//						string[] dirs = bundle.dirs;
//
//						Bundle newBundle = new Bundle();
//						string newBundleName = enumerator.Current.Key + BundleManager.sceneConfigBundleSuffix;
//						newBundle.is_local = bundle.is_local;
//						newBundle.dirs = new string[dirs.Length];
//						for(int k = 0; k < dirs.Length; k++)
//						{
//							newBundle.dirs[k] = dirs[k] + BundleManager.sceneBundleDependenceSuffix;
//						}
//
//						newBundleLayer.Add(newBundleName, newBundle);
//					}
//				}
//
//				Dictionary<string, Bundle>.Enumerator newEnumerator = newBundleLayer.GetEnumerator();
//				while(newEnumerator.MoveNext())
//				{
//					layer.Add(newEnumerator.Current.Key, newEnumerator.Current.Value);
//				}
//			}
//
//			Dictionary<string, BundleGroup>.Enumerator groupEnumerator = config.groups.GetEnumerator();
//			while(groupEnumerator.MoveNext())
//			{
//				BundleGroup bundleGroup = groupEnumerator.Current.Value;
//
//				if(bundleGroup.bundles != null)
//				{
//					List<string> newBundles = new List<string>(bundleGroup.bundles);
//					for(int j = 0; j < bundleGroup.bundles.Length; j++)
//					{
//						string bundleName = bundleGroup.bundles[j];
//						Bundle bundle = config.getBundle(bundleName);
//						if(bundle.type == BundleType.scene)
//						{
//							newBundles.Add(bundleName + BundleManager.sceneConfigBundleSuffix);
//						}
//					}
//					bundleGroup.bundles = newBundles.ToArray();
//				}
//			}
//
//			MemoryStream stream = new MemoryStream();
//			BinaryWriter writer = new BinaryWriter(stream);
//			config.Write(writer);
//
//			string 	configPath = EditorCommonTool.getBundleConfigPath(targets[i]);
//			string dir = Path.GetDirectoryName(configPath);
//			Directory.CreateDirectory(dir);
//			CommonUtil.bytesToFile(stream.ToArray(), configPath);
//
//			if(ExportAssetBundles.createJson)
//			{
//				string newJsonStr = JsonConvert.SerializeObject(config, Formatting.Indented);
//				configPath = configPath.Replace(".bytes", "_json.json");
//				CommonUtil.stringToFile(newJsonStr, configPath);
//			}
//		}
//
//		AssetDatabase.Refresh();
//
//		ExportAssetBundles.CreateDependenceFiles();
//	}

	public static SpriteSheetConfig CreateRawSpriteSheetConfig(string resPath)
	{
		SpriteSheetConfig config = EditorCommonTool.getRawSpriteSheetConfig(resPath);

		Dictionary<string, SpriteSheetInfo> newInfos = new Dictionary<string, SpriteSheetInfo>();
		SpriteSheetConfig.Enumerator enumerator2 = config.GetEnumerator();
		while(enumerator2.MoveNext())
		{
			string name = enumerator2.Current.Key;
			SpriteSheetInfo info = enumerator2.Current.Value;
			List<string> subPacks = new List<string>();

			bool jpg = info.isJpg(resPath);
			bool png = info.isPng(resPath);

			if(png)
			{
				string ext = ".png";
				string newName = name + "_rgba";
				SpriteSheetInfo newInfo = new SpriteSheetInfo();
				newInfo.is_sub_pack = true;

				newInfo.heuristic_mask = false;
				newInfo.edge_extrude = info.edge_extrude;
				if(newInfo.edge_extrude <= 0)
				{
					newInfo.edge_extrude = 1;
				}
				newInfo.is_from_interface = info.is_from_interface;
				newInfo.alpha_threshold = info.alpha_threshold;
				newInfo.split_alpha_type = info.split_alpha_type;
				newInfo.multi_pack = info.multi_pack;
				newInfo.max_size = info.max_size;
				newInfo.scale = info.scale;

				if(jpg)
				{
					newInfo.dirs = info.getFiles(resPath, ext, true).ToArray();
				}
				else
				{
					newInfo.dirs = info.dirs;
				}
				newInfos.Add(newName, newInfo);
				subPacks.Add(newName);
			}


			if(jpg)
			{
				string ext = ".jpg";
				string newName = name + "_rgb";
				SpriteSheetInfo newInfo = new SpriteSheetInfo();
				newInfo.is_sub_pack = true;
				
				newInfo.heuristic_mask = info.heuristic_mask;
				newInfo.edge_extrude = info.edge_extrude;
				if(newInfo.edge_extrude <= 0)
				{
					newInfo.edge_extrude = 1;
				}
				newInfo.is_from_interface = info.is_from_interface;
				newInfo.alpha_threshold = info.alpha_threshold;
				newInfo.split_alpha_type = SplitAlphaType.NONE;
				newInfo.multi_pack = info.multi_pack;
				newInfo.max_size = info.max_size;
				newInfo.scale = info.scale;

				if(png)
				{
					newInfo.dirs = info.getFiles(resPath, ext, true).ToArray();
				}
				else
				{
					newInfo.dirs = info.dirs;
				}
				newInfos.Add(newName, newInfo);
				subPacks.Add(newName);
			}

			info.sub_pack_names = subPacks.ToArray();
		}

		Dictionary<string, SpriteSheetInfo>.Enumerator enumerator3 = newInfos.GetEnumerator();
		while(enumerator3.MoveNext())
		{
			config.Add(enumerator3.Current.Key, enumerator3.Current.Value);
		}

		return config;
	}

	[MenuItem("Custom/OldProject/DeletePlayerPrefs")]
	public static void DeletePlayerPrefs()
	{
		PlayerPrefs.DeleteAll();
	}

	[MenuItem("Assets/Custom/OldProject/ConvertSpriteSheetConfigToJsonFile")]
	public static void ConvertSpriteSheetConfigToJsonFile()
	{
		string path = AssetDatabase.GetAssetPath(Selection.activeObject);
		byte[] data = CommonUtil.fileToBytes(path);
		if(data == null)
		{
			return;
		}
		SpriteSheetConfig config = new SpriteSheetConfig();
		MemoryStream stream = new MemoryStream(data);
		BinaryReader reader = new BinaryReader(stream);
		config.Read(reader);

		string newPath = path.Replace(".bytes", "_json");
		string str = JsonConvert.SerializeObject(config, Formatting.Indented);
		CommonUtil.stringToFile(newPath, str);
		AssetDatabase.Refresh();
	}

	[MenuItem("Assets/Custom/OldProject/ConvertSpriteSheetLoaderConfigToJsonFile")]
	public static void ConvertSpriteSheetLoaderConfigToJsonFile()
	{
		string path = AssetDatabase.GetAssetPath(Selection.activeObject);
		byte[] data = CommonUtil.fileToBytes(path);
		if(data == null)
		{
			return;
		}
		MemoryStream stream = new MemoryStream(data);
		BinaryReader reader = new BinaryReader(stream);
		SpriteSheetLoader config = new SpriteSheetLoader(reader);
		
		string newPath = path.Replace(".bytes", "_json");
		string str = JsonConvert.SerializeObject(config, Formatting.Indented);
		CommonUtil.stringToFile(newPath, str);
		AssetDatabase.Refresh();
	}

	[MenuItem("Assets/Custom/OldProject/ConvertBundleConfigToJsonFile")]
	public static void ConvertBundleConfigToJsonFile()
	{
		string path = AssetDatabase.GetAssetPath(Selection.activeObject);
		byte[] data = CommonUtil.fileToBytes(path);
		if(data == null)
		{
			return;
		}

		BundleConfig config = BundleConfig.createByBytes(data);
//		MemoryStream stream = new MemoryStream(data);
//		BinaryReader reader = new BinaryReader(stream);
//		BundleConfig config = new BundleConfig();
//		config.Read(reader);

		string newPath = path.Replace(".bytes", "_json");
		string str = JsonConvert.SerializeObject(config, Formatting.Indented);
		CommonUtil.stringToFile(newPath, str);
		AssetDatabase.Refresh();
	}
}